/* PhotoCursorAdapter.java
 * Adapts photo database cursor for use in other code
 */
package com.tagit;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.tagit.PhotosDbAdapter;

public class PhotoCursorAdapter extends BaseAdapter {

	private Cursor mCursor;
	private Context mContext;
	private int mState;
	private ArrayList<Long> mSelected; // Currently selected photos
	private ArrayList<Long> mRejected; // Currently rejected photos

	// Initializes variables for use in other adapter functions
	public PhotoCursorAdapter(Context context, Cursor cursor, int state) {
		mContext = context;
		mCursor = cursor;
		mState = state;
		mSelected = new ArrayList<Long>();
		mRejected = new ArrayList<Long>();
	}

	// Return list of selected photos
	public ArrayList<Long> getMSelected() {
		return mSelected;
	}

	// Return total count of photos
	public int getCount() {
		return mCursor.getCount();
	}

	// Returns a given URI given a position
	public String getItem(int position) {
		mCursor.moveToPosition(position);
		if (mCursor.isBeforeFirst() || mCursor.isAfterLast())
			return null;
		String path = mCursor.getString(mCursor
				.getColumnIndex(PhotosDbAdapter.KEY_URI));
		return path;
	}

	// Returns a photo ID given a position
	public long getItemId(int position) {
		// Extract data from the cursor.
		mCursor.moveToPosition(position);
		if (mCursor.isBeforeFirst() || mCursor.isAfterLast())
			return -1;
		return mCursor.getLong(mCursor.getColumnIndex(PhotosDbAdapter.KEY_ID));
	}

	// Return view (including background if photo currently selected)
	public View getView(int position, View convertView, ViewGroup parent) {
		long check = getItemId(position);

		// Set path and imageview
		String path = (String) getItem(position);
		ImageView imView;

		if (convertView == null) {
			// New view needs to be created
			imView = new ImageView(mContext);
			imView.setLayoutParams(new GridView.LayoutParams(85, 85));
			imView.setPadding(4, 4, 4, 4);
		} else {
			imView = (ImageView) convertView;
		}

		Bitmap bitmap = BitmapFactory.decodeFile(path);

		/*
		 * Check state - if in SelectMode, draw view with boxes, otherwise draw
		 * normal Code to resize pictures to squares for borders. Based on code
		 * from
		 * http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
		 */
		Matrix matrix = new Matrix();
		int height = bitmap.getHeight();
		int width = bitmap.getWidth();
		int newWidth = 75;
		int newHeight = 75;

		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;

		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		imView.setScaleType(ScaleType.CENTER);
		imView.setImageBitmap(resizedBitmap);

		if (mState == 1) {

			if (mSelected.contains(check)) {
				imView.setBackgroundResource(R.drawable.neonbox);
			} else {
				imView.setBackgroundResource(R.drawable.graybox);
			}
		}
		return imView;

	}

	// Set the state to select state or not
	public int setState(int newState) {
		mState = newState;
		return mState;
	}

	// Add and remove selected photos to the "selected" arrayList by ID
	public void addSelected(long ID) {
		mSelected.add(ID);
	}

	// Remove photo by ID from list of selected photos
	public void removeSelected(long ID) {
		mSelected.remove(ID);
	}

	// Determine if photo, by ID, is currently selected
	public boolean isSelected(long ID) {
		if (mSelected.contains(ID)) {
			return true;
		} else {
			return false;
		}

	}

	// Add a tag to a photo
	public boolean addTag(String tag, long photoID) {
		PhotosDbAdapter mDb = new PhotosDbAdapter(mContext);
		mDb.open();
		if (tag.length() == 0) {
			return true;
		}

		mDb.addTagToPhoto(photoID, tag);
		return true;
	}

	// Add a tag to all selected photos
	public boolean addTagToSelected(String tag) {
		for (int i = 0; i < mSelected.size(); i++) {
			addTag(tag, mSelected.get(i));
		}
		return true;
	}

	// Return selected photos
	public ArrayList<Long> getSelected() {
		return mSelected;
	}

}
